﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class customLensManager : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack) {
            CSUtils.CheckSession();
            bindRefractive();
            this.binddata();            
            PanelInput.Visible = false;
        }
    }


    protected void ButtonQuery_Click(object sender, EventArgs e)
    {
        binddata();
    }

    private void bindRefractive()
    {
        DropDownListrRefractive.DataSource = CSUtils.getCustomLensIndex("");
        DropDownListrRefractive.DataBind();

        DropDownListrRefractive_Q.DataSource = CSUtils.getCustomLensIndex("");
        DropDownListrRefractive_Q.DataBind();

    }


   

    private void binddata() {
        GridView1.DataSource = this.getAllLensBase();
        GridView1.DataBind();
    }


    private DataSet getAllLensBase() {
        string sql =
            "select * from cs_custom_lens_base {0} " +
            "order by lens_id";

        string where = "where 1 = 1 ";

        string lensName = TextBoxLensName_Q.Text;
        if (lensName.Trim() != "")
        {
            where += "and lens_name like '%" + lensName + "%' ";
        }

        string refractive = DropDownListrRefractive_Q.SelectedValue;
        if (refractive.Trim() != "-1")
        {
            where += "and refractive_index = '" + refractive + "' ";
        }

        sql = string.Format(sql, where);

        DataSet ds = new DataSet();
        DBConn myDB = new DBConn();
        ds = myDB.getDataSet(sql);
        myDB.Close();
        return ds;
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) {
        GridView1.PageIndex = e.NewPageIndex;
        this.binddata();
    }


    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {

        if (e.CommandName.Equals("command_edit") ||
            e.CommandName.Equals("command_add"))
        {
            PanelInput.Visible = true;
            TextBoxCommandType.Text = e.CommandName;
            LabelLensID.Text = e.CommandArgument.ToString();
            CustomLensVO vo = CSUtils.getCustomLens(LabelLensID.Text);
            TextBoxLensName.Text = vo.Name;
            DropDownListLensType.SelectedValue = vo.Type;
            DropDownListrRefractive.SelectedValue = vo.RefractiveIndex;
            TextBoxMaterial.Text = vo.LensMaterial;
            DropDownListFilm.SelectedValue = vo.FilmColor;
            DropDownListColor.SelectedValue = vo.LensColor;            
            DropDownListLensSource.SelectedValue = vo.LensSource;
            if (vo.LensSource == "创天")
            {
                TextBoxSourceLens.Text = vo.SourceLensID;
                LabelSourceLens.Visible = true;
                TextBoxSourceLens.Visible = true;
            }
            else
            {
                LabelSourceLens.Visible = false;
                TextBoxSourceLens.Visible = false;
            }

        }
        else if (e.CommandName.Equals("command_visible"))
        {

            string id = e.CommandArgument.ToString();

            if (this.updateLensVisible(id))
            {
                //JScript.Alert("保存成功！");
            }
            else
            {
                JScript.Alert("保存失败！");
            }

            this.binddata();
        }
        else
        {
            this.cleanPanelInput();
        }
        
        
    }


    protected void ButtonSubmit_Click(object sender, EventArgs e) {
        
        string lensID = LabelLensID.Text;
        string lensName = TextBoxLensName.Text;
        string lensType = DropDownListLensType.SelectedValue;
        string refractiveIndex = DropDownListrRefractive.SelectedValue;
        string lensMaterial = TextBoxMaterial.Text;
        string filmColor = DropDownListFilm.SelectedValue;
        string lensColor = DropDownListColor.SelectedValue;
        string lensSource = DropDownListLensSource.SelectedValue;
        string sourceLensID = TextBoxSourceLens.Text;

        string commandType = TextBoxCommandType.Text;
        CustomLensVO vo = null;
        if (commandType.Equals("command_edit"))
        {
            //get vo from db
            vo = CSUtils.getCustomLens(lensID);

        }
        else if (commandType.Equals("command_add"))
        {
            // new one
            vo = new CustomLensVO();            
        }

        vo.Name = lensName;
        vo.Type = lensType;
        vo.RefractiveIndex = refractiveIndex;
        vo.LensMaterial = lensMaterial;
        vo.FilmColor = filmColor;
        vo.LensColor = lensColor;
        vo.LensSource = lensSource;
        if (lensSource == "创天")
        {
            vo.GoodsID = "null";
            vo.SourceBrandID = "1"; // 白包装ID
            vo.SourceLensID = sourceLensID;
            
        }
        else
        {
            vo.GoodsID = "null";
            vo.SourceBrandID = "null";
            vo.SourceLensID = "null";
        }

        if (CSUtils.mergeCustomLens(vo))
        {
            //JScript.Alert("保存成功！");
        }
        else
        {
            JScript.Alert("保存失败！");
        }

        this.binddata();
        
    }

    protected void LinkButtonAdd_Click(object sender, EventArgs e) {
        PanelInput.Visible = true;
        TextBoxCommandType.Text = "command_add";
        this.cleanPanelInput();

    }



    private void cleanPanelInput() {
        LabelLensID.Text = "";
        TextBoxLensName.Text = "";
        DropDownListLensType.SelectedIndex = 0;
        DropDownListrRefractive.SelectedIndex = 0;
        TextBoxMaterial.Text = "";
        DropDownListFilm.SelectedIndex = 0;
        DropDownListColor.SelectedIndex = 0;
        DropDownListLensSource.SelectedIndex = 0;
        TextBoxSourceLens.Text = "";
        LabelSourceLens.Visible = true;
        TextBoxSourceLens.Visible = true;
    }

    protected void DropDownListLensSource_SelectedIndexChanged(object sender, EventArgs e)
    {
        setLensSource();
    }

    private void setLensSource()
    {
        string sel = DropDownListLensSource.SelectedValue;
        if (sel == "创天")
        {
            LabelSourceLens.Visible = true;
            TextBoxSourceLens.Visible = true;
        }
        else
        {
            LabelSourceLens.Visible = false;
            TextBoxSourceLens.Visible = false;
        }

    }

    private bool updateLensVisible(string lens_id)
    {

        string sqlUpdate =
            "update cs_custom_lens_base " +
            "set visible = visible-1 " +
            "where lens_id = " + lens_id;

        DBConn myDB = new DBConn();
        int tmp = myDB.Update(sqlUpdate);
        myDB.Close();
        return (tmp == 1);

    }
}
